package com.ccjq.shosestore.shoppingcart.dao.impl;

import com.ccjq.shosestore.core.dbmanger.DbConnection;
import com.ccjq.shosestore.shoppingcart.dao.CarDao;
import com.ccjq.shosestore.shoppingcart.po.CarPo;
import com.ccjq.shosestore.shoppingcart.po.ProductCarPo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CarDaoImpl implements CarDao {

    public int insertAll(CarPo carPo) {
        int result = 0;
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet res = null;
        String sql = "insert into t_shopping_car values(?,?,?,?)";
        try {
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1, carPo.getShopping_cart_id());
            pre.setString(2, carPo.getProduct_id());
            pre.setInt(3, carPo.getQuantity());
            pre.setString(4, carPo.getUser_id());
            result = pre.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(null, pre, con);
        }
        return result;
    }

    public int deleteCar(String shopping_cart_id) {
        int result = 0;
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet res = null;
        String sql = "delete from t_shopping_car where shopping_cart_id=?";
        try {
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1, shopping_cart_id);
            result = pre.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(null, pre, con);
        }
        return result;
    }

    public int updateCar(int quantity, String shopping_cart_id) {
        int result = 0;
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet res = null;
        String sql = "update t_shopping_car set quantity=? where shopping_cart_id=?";
        try {
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setInt(1, quantity);
            pre.setString(2, shopping_cart_id);
            result = pre.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(null, pre, con);
        }
        return result;
    }

    public List<ProductCarPo> selectCar(String user_id) {
        List<ProductCarPo> list = new ArrayList<ProductCarPo>();
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet res = null;
        String sql = "select a.product_id,description,price,model,feature,imgpath,\n" +
                "movalty_status,publish_date,category_id,shopping_cart_id,\n" +
                "quantity,user_id from t_shopping_car a full join t_product b on a.product_id=b.product_id where user_id=?";
        try {
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1, user_id);
            res = pre.executeQuery();
            while(res.next()){
                ProductCarPo carPo = new ProductCarPo();
                carPo.setProduct_id(res.getString(1));
                carPo.setDescription(res.getString(2));
                carPo.setPrice(res.getDouble(3));
                carPo.setModel(res.getString(4));
                carPo.setFeature(res.getString(5));
                carPo.setImgpath(res.getString(6));
                carPo.setMovalty_status(res.getInt(7));
                carPo.setPublish_date(res.getString(8));
                carPo.setCategory_id(res.getString(9));
                carPo.setShopping_cart_id(res.getString(10));
                carPo.setQuantity(res.getInt(11));
                carPo.setUser_id(res.getString(12));
                list.add(carPo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(res, pre, con);
        }
        return list;
    }

    public int selectStock(String product_id) {
        int result = 0;
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet res = null;
        String sql = "select MAX(quantity) from t_stock where product_id=?";
        try {
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1, product_id);
            res = pre.executeQuery();
            if(res.next()){
                result= res.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(null, pre, con);
        }
        return result;
    }
}
